#include "ServerSelectScene.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CCSGUIReader.h"
#include "../Networks/NetworkProtocolMud.h"
#include "../Networks/NetworkManager.h"

#define SERVER_NAME_LABEL_TAG_BEGIN		101

ServerSelectScene* ServerSelectScene::m_pInstance = 0;
ServerSelectScene::ServerSelectScene()
{
	m_pInstance = this;
	m_pServerMsgLabel = 0;
}

ServerSelectScene::~ServerSelectScene()
{
	m_pInstance = 0;
}

ServerSelectScene* ServerSelectScene::getInstance()
{
	return m_pInstance;
}

cocos2d::Scene* ServerSelectScene::scene()
{
	// 'scene' is an autorelease object
	cocos2d::Scene* scene = cocos2d::Scene::create();

	// 'layer' is an autorelease object
	ServerSelectScene *layer = ServerSelectScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool ServerSelectScene::init()
{
	if (!Layer::init())
	{
		return false;
	}

	cocos2d::ui::Widget* rootLayout = (cocostudio::GUIReader::getInstance()->widgetFromJsonFile("ServerListUI.ExportJson"));
	if (!rootLayout) return false;
	cocos2d::ui::Widget* serverListPanel = rootLayout->getChildByName("ServerListPanel");
	if (serverListPanel)
	{
		m_pServerMsgLabel = dynamic_cast<cocos2d::ui::Text*>(serverListPanel->getChildByName("ServerMsgLabel"));
		cocos2d::ui::Button* closeButton = dynamic_cast<cocos2d::ui::Button*>(serverListPanel->getChildByName("CloseButton"));
		closeButton->addTouchEventListener(this, (cocos2d::ui::SEL_TouchEvent)&ServerSelectScene::eventCloseButton);
		char label_control_name[1024];
		for (int i = 0; i < SERVER_LIST_NAME_LABEL_COUNT; i++)
		{
			sprintf(label_control_name, "ServerNameLabel%02d", i + 1);
			m_pServerNameLabel[i] = dynamic_cast<cocos2d::ui::Text*>(serverListPanel->getChildByName(label_control_name));
			m_pServerNameLabel[i]->addTouchEventListener(this, (cocos2d::ui::SEL_TouchEvent)&ServerSelectScene::eventServerNameLabel);
		}
		
	}
	/*if (loginPanel)
	{
		cocos2d::ui::Button* closeButton = dynamic_cast<cocos2d::ui::Button*>(loginPanel->getChildByName("CloseButton"));
		closeButton->addTouchEventListener(this, (cocos2d::ui::SEL_TouchEvent)&LoginScene::eventCloseButton);
		m_pAccountText = dynamic_cast<cocos2d::ui::TextField*>(loginPanel->getChildByName("AccountText"));
		m_pAccountText->addEventListenerTextField(this, (cocos2d::ui::SEL_TextFieldEvent)&LoginScene::eventAccountText);
		m_pPasswordText = dynamic_cast<cocos2d::ui::TextField*>(loginPanel->getChildByName("PasswordText"));
		m_pPasswordText->addEventListenerTextField(this, (cocos2d::ui::SEL_TextFieldEvent)&LoginScene::eventPasswordText);
		cocos2d::ui::Button* loginButton = dynamic_cast<cocos2d::ui::Button*>(loginPanel->getChildByName("LoginButton"));
		loginButton->addTouchEventListener(this, (cocos2d::ui::SEL_TouchEvent)&LoginScene::eventLoginButton);
	}
#ifdef WIN32
	cocos2d::EventListenerKeyboard* listenerKeyboard = cocos2d::EventListenerKeyboard::create();
	listenerKeyboard->onKeyPressed = CC_CALLBACK_2(LoginScene::eventKeyboardPressed, this);
	cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
#endif */
	addChild(rootLayout);

	return true;
}

void ServerSelectScene::setServerMessage(const std::string& msg)
{
	if (m_pServerMsgLabel)
	{
		m_pServerMsgLabel->setText(msg);
	}
}

void ServerSelectScene::setServerNameCapacity(int idx, const std::string& name, int cap)
{
	if (idx > SERVER_LIST_NAME_LABEL_COUNT) return;
	if (!m_pServerNameLabel[idx - 1]) return;
	char ss[64];
	sprintf(ss, "%d", cap);
	std::string name_label = name + "(" + ss + "%)";
	m_pServerNameLabel[idx - 1]->setText(name_label);
}

void ServerSelectScene::eventCloseButton(cocos2d::Ref* sender, cocos2d::ui::TouchEventType type)
{
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_BEGAN:
		cocos2d::Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
		exit(0);
#endif
		break;
	default:
		break;
	}
}

void ServerSelectScene::eventServerNameLabel(cocos2d::Ref* sender, cocos2d::ui::TouchEventType type)
{
	cocos2d::ui::Text* labelServerName = dynamic_cast<cocos2d::ui::Text*>(sender);
	if (!labelServerName) return;
	int tag = labelServerName->getTag();
	if (tag - SERVER_NAME_LABEL_TAG_BEGIN >= SERVER_LIST_NAME_LABEL_COUNT) return;
}

